package com.eightbears.beboy.base.util;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.eightbears.beboy.R;
import com.eightbears.beboy.base.util.okhttp.OkHttpUtils;
import com.eightbears.beboy.base.util.okhttp.callback.StringCallback;
import com.eightbears.beboy.base.util.shared.ui.ShareBoard;
import com.eightbears.beboy.base.util.shared.ui.ShareBoardlistener;
import com.eightbears.beboy.base.util.shared.ui.SnsPlatform;
import com.eightbears.beboy.entity.CodeEntity;
import com.eightbears.beboy.entity.ShareCallBackEntity;
import com.eightbears.beboy.entity.ShareEntity;
import com.socks.library.KLog;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import cn.jiguang.share.android.api.JShareInterface;
import cn.jiguang.share.android.api.PlatActionListener;
import cn.jiguang.share.android.api.Platform;
import cn.jiguang.share.android.api.ShareParams;
import cn.jiguang.share.qqmodel.QQ;
import cn.jiguang.share.qqmodel.QZone;
import cn.jiguang.share.wechat.Wechat;
import cn.jiguang.share.wechat.WechatFavorite;
import cn.jiguang.share.wechat.WechatMoments;
import okhttp3.Call;

import static com.eightbears.beboy.base.util.CommonAPI.CONNECTION_STATUS_SUCCESS;
import static com.eightbears.beboy.base.util.CommonAPI.ERROR_CODE_LOGIN_NICKNAME_50001;
import static com.eightbears.beboy.base.util.CommonAPI.ERROR_CODE_LOGIN_NICKNAME_50002;
import static com.eightbears.beboy.base.util.CommonAPI.ERROR_CODE_LOGIN_NICKNAME_50004;
import static com.eightbears.beboy.base.util.CommonAPI.KEY_HTTP_HEADER;
import static com.eightbears.beboy.base.util.CommonAPI.URL_SHARE;
import static com.eightbears.beboy.base.util.CommonAPI.URL_SHARE_CALL_BACK;
import static com.eightbears.beboy.ui.user.fragment.HomeFragment.EVENT_SHARED_SUCCESS;

/**
 * Created by baoga on 2017/11/26.
 */

public class SharesUtil {


    private static String postId;
    private static ShareBoard mShareBoard;
    private static int mAction = Platform.ACTION_SHARE;
    private static ShareEntity shareEntity;
    private static Activity mActivity;
    private static boolean isAppShared = false;
    private static CodeEntity mCodeEntity;
    private static SnsPlatform snsPlatform;

    /**
     * 分享帖子内容
     */
    public static void shareContent(final Activity activity, final String postid) {
        mActivity = activity;
        isAppShared = true;
        OkHttpUtils.get().url(URL_SHARE)
                .addHeader(KEY_HTTP_HEADER, "Bearer " + SPUtil.getAccessToken())
                .build().execute(new StringCallback() {

            @Override
            public void onResponse(String response) {
                shareEntity = GsonUtil.fromJson(response, ShareEntity.class);
                if (shareEntity.getStatus_code().equals(CONNECTION_STATUS_SUCCESS)) {
                    postId = postid;
                    showBroadView();
                }
            }

            @Override
            public void onError(Call call, Exception e) {
                KLog.e(e.getMessage());
            }
        });
    }

    public static void sharedInviteBoard(Activity activity, CodeEntity codeEntity) {
        mActivity = activity;
        isAppShared = false;
        mCodeEntity = codeEntity;
        if (codeEntity == null) {
            ShowToast.showShortToast(mActivity.getString(R.string.get_failed_please_check));
            return;
        }
        showBroadView();
    }

    private static void showBroadView() {
        mShareBoard = new ShareBoard(mActivity);
        List<String> platforms = JShareInterface.getPlatformList();
        if (platforms != null) {
            Iterator var2 = platforms.iterator();
            while (var2.hasNext()) {
                String temp = (String) var2.next();
                if(isAppShared) {
                    if(!temp.equals(QQ.Name) && !temp.equals(QZone.Name)){
                        snsPlatform = createSnsPlatform(temp);
                        mShareBoard.addPlatform(snsPlatform);
                    }
                } else {
                    snsPlatform = createSnsPlatform(temp);
                    mShareBoard.addPlatform(snsPlatform);
                }
            }
        }
        mShareBoard.setShareboardclickCallback(mShareBoardlistener);
        mShareBoard.show();
    }

    public static SnsPlatform createSnsPlatform(String platformName) {
        String mShowWord = platformName;
        String mIcon = "";
        String mGrayIcon = "";
        String mKeyword = platformName;
        if (platformName.equals(Wechat.Name)) {
            mIcon = "jiguang_socialize_wechat";
            mGrayIcon = "jiguang_socialize_wechat";
            mShowWord = "jiguang_socialize_text_weixin_key";
        } else if (platformName.equals(WechatMoments.Name)) {
            mIcon = "jiguang_socialize_wxcircle";
            mGrayIcon = "jiguang_socialize_wxcircle";
            mShowWord = "jiguang_socialize_text_weixin_circle_key";

        } else if (platformName.equals(WechatFavorite.Name)) {
            mIcon = "jiguang_socialize_wxfavorite";
            mGrayIcon = "jiguang_socialize_wxfavorite";
            mShowWord = "jiguang_socialize_text_weixin_favorite_key";

        }
        if (!isAppShared) {
            if (platformName.equals(QQ.Name)) {
                mIcon = "jiguang_socialize_qq";
                mGrayIcon = "jiguang_socialize_qq";
                mShowWord = "jiguang_socialize_text_qq_key";

            } else if (platformName.equals(QZone.Name)) {
                mIcon = "jiguang_socialize_qzone";
                mGrayIcon = "jiguang_socialize_qzone";
                mShowWord = "jiguang_socialize_text_qq_zone_key";
            }
        }

        return ShareBoard.createSnsPlatform(mShowWord, mKeyword, mIcon, mGrayIcon, 0);
    }

    private static ShareBoardlistener mShareBoardlistener = new ShareBoardlistener() {
        @Override
        public void onclick(SnsPlatform snsPlatform, String platform) {
            String title, text, url;
            Bitmap imageData;

            if (isAppShared) {
                title = shareEntity.getData().getShare_title();
                text = shareEntity.getData().getShare_text();
                url = shareEntity.getData().getShare_url();
                imageData = BitmapFactory.decodeResource(mActivity.getResources(), R.mipmap.icon);
            } else {
                title = mActivity.getString(R.string.text_invite_share);
                text = mActivity.getString(R.string.text_invite_code_show) + mCodeEntity.getData().getCode();
                url = CommonAPI.URL_SHARE_INVITE;
                imageData = BitmapFactory.decodeResource(mActivity.getResources(), R.mipmap.icon_invite_share);
            }

            ShareParams shareParams = new ShareParams();
            shareParams.setShareType(Platform.SHARE_WEBPAGE);
            shareParams.setTitle(title);
            shareParams.setText(text);
            shareParams.setUrl(url);
            shareParams.setImageData(imageData);
            JShareInterface.share(platform, shareParams, mShareListener);
        }
    };

    private static PlatActionListener mShareListener = new PlatActionListener() {
        @Override
        public void onComplete(Platform platform, int action, HashMap<String, Object> data) {
            mShareBoard.close();
            shareCallback();
        }

        @Override
        public void onError(Platform platform, int action, int errorCode, Throwable error) {
            mShareBoard.close();
            KLog.e(action + "/" + errorCode + error.getMessage());
        }

        @Override
        public void onCancel(Platform platform, int action) {
            mShareBoard.close();
            ShowToast.showShortToast(mActivity.getString(R.string.text_cancel_shared));
        }
    };


    /**
     * 分享回调
     */
    private static void shareCallback() {
        String url = URL_SHARE_CALL_BACK + postId + "/shared";
        OkHttpUtils.post().url(url)
                .addHeader(KEY_HTTP_HEADER, "Bearer " + SPUtil.getAccessToken())
                .build().execute(new StringCallback() {
            @Override
            public void onResponse(String response) {
                ShareCallBackEntity shareCallBackEntity = GsonUtil.fromJson(response, ShareCallBackEntity.class);
                if (shareCallBackEntity.getStatus_code().equals(CONNECTION_STATUS_SUCCESS)) {
                    EventBus.getDefault().post(EVENT_SHARED_SUCCESS);
                    if (shareCallBackEntity.getData().getHong_bao() > 0) {
                        ShowToast.showShortToast("恭喜你获得[" + shareCallBackEntity.getData().getHong_bao() + "]个红包");
                    } else {
                        switch (shareCallBackEntity.getData().getCode()) {
                            case ERROR_CODE_LOGIN_NICKNAME_50001:
                                ShowToast.showShortToast(mActivity.getString(R.string.text_areadle_over));
                                return;
                            case ERROR_CODE_LOGIN_NICKNAME_50002:
                                ShowToast.showShortToast(mActivity.getString(R.string.text_today_areadle_get));
                                return;
                            case ERROR_CODE_LOGIN_NICKNAME_50004:
                                ShowToast.showShortToast(mActivity.getString(R.string.text_areadle_get));
                                return;
                        }
                    }
                }
            }

            @Override
            public void onError(Call call, Exception e) {
                KLog.e(e.getMessage());
            }
        });
    }
}
